package linkedList;

public class Solution2 {
    public ListNode removeElements(ListNode head, int val){
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;

        ListNode prev = dummyNode;
        while (prev.next != null){
            if(prev.next.val == val){
                prev.next = prev.next.next;
            }else{
                prev = prev.next;
            }
        }
        return dummyNode.next;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,6,3,4,6,6};
        ListNode head = new ListNode(arr);
        System.out.println(head);
        Solution2 solution = new Solution2();
        ListNode res  = solution.removeElements(head,6);
        System.out.println(res);
    }
}
